package com.hauwei.huaweishop.controller;

import com.hauwei.huaweishop.entity.Orders;
import com.hauwei.huaweishop.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/orders")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    /**
     * 查找所有信息
     * @return
     */
    @GetMapping("/findAllOrders")
    public Map<String, Object> findAllOrders(){
        Map<String, Object> resultMap = new HashMap<>();
        try {
            // 查询员工的信息
            List<Orders> ordersList = ordersService.queryOrdersList();
            resultMap.put("records", ordersList);
            resultMap.put("status", true);
            resultMap.put("code", 200);
            resultMap.put("msg", "商品订单查询成功");
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("status", false);
            resultMap.put("code", 500);
            resultMap.put("msg", "商品订单查询失败");
        }
        return resultMap;
    }

    /**
     * 通过id查询信息
     * @param id
     * @return
     */
    @GetMapping("/findOneShopTyepsById")
    public Map<String, Object> findOneById(Integer id){
        Map<String, Object> resultMap = new HashMap<>();
        try {
            Orders ordersById = ordersService.queryByOid(id);
            resultMap.put("recorde", ordersById);
            resultMap.put("status", true);
            resultMap.put("code", 200);
            resultMap.put("msg", "商品订单查询成功");
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("status", false);
            resultMap.put("code", 500);
            resultMap.put("msg", "商品订单查询失败");
        }
        return resultMap;
    }

    /**
     * 添加方法
     * @param orders
     * @return
     */
    @PostMapping("/addOrders")
    public Map<String, Object> addOrders(Orders orders){
        Map<String, Object> resultMap = new HashMap<>();
        log.info("商品订单信息：[{}]", orders);
        try {
            // 新增信息
            ordersService.addOrders(orders);
            resultMap.put("status", true);
            resultMap.put("code", 200);
            resultMap.put("msg", "商品订单增加成功");
        }catch (Exception e){
            resultMap.put("status", false);
            resultMap.put("code", 500);
            resultMap.put("msg", "商品订单增加失败");
        }
        return resultMap;
    }

    /**
     * 根据id删除信息
     * @param id
     * @return
     */
    @GetMapping("/deletOrdersById")
    public Map<String, Object> deletOrdersById(Integer id){
        Map<String, Object> resultMap = new HashMap<>();
        log.info("删除信息id：[{}]", id);
        try {
            // 删除信息
            ordersService.deleteOrdersByOid(id);
            resultMap.put("status", true);
            resultMap.put("code", 200);
            resultMap.put("msg", "商品订单删除成功");
        }catch (Exception e){
            resultMap.put("status", false);
            resultMap.put("code", 500);
            resultMap.put("msg", "商品订单删除失败");
        }
        return resultMap;
    }

    /**
     * 修改信息
     * @param orders
     * @return
     */
    @RequestMapping("/updateOrders")
    public Map<String, Object> updateOrders(Orders orders){
        Map<String, Object> resultMap = new HashMap<>();
        log.info("修改信息：[{}]", orders);
        try {
            // 修改信息
            ordersService.updateOrders(orders);
            resultMap.put("status", true);
            resultMap.put("code", 200);
            resultMap.put("msg", "商品订单修改成功");
        }catch (Exception e){
            resultMap.put("status", false);
            resultMap.put("code", 500);
            resultMap.put("msg", "商品订单修改失败");
        }
        return resultMap;
    }
}
